# -*- coding: utf-8 -*-
from flask import Blueprint,render_template
from common.models.user import User
from common.libs.Helper import ops_renderJSON,ops_renderErrJSON,ops_render
from common.models.document import Document
from flask import Blueprint, request
from common.libs.Helper import ops_renderJSON, ops_renderErrJSON, iPagenation, ops_render
from qiniu import Auth
from qiniu import put_data
from qiniu import BucketManager
import os
from application import db, app
from common.libs.DataHelper import getCurrentTime

index_page = Blueprint( "index_page",__name__ )

@index_page.route("/")
def login():
    return ops_render( "/member/login1.html")

@index_page.route("/index")
def login1():
    return ops_render( "/member/login.html")


def pagesadmin(index):
    req = request.values
    page = 1
    if 'p' in req and req['p']:
        page = int(req['p'])

    page_params = {
        'total_count': index.count(),
        "page_size": 8,
        'page': page,
        'url': "/index?",
    }
    pages = iPagenation(page_params)
    # -1 - 30,30 - 60 ,60 - 90
    offset = (page - 1) * page_params['page_size']
    limit = page * page_params['page_size']
    index = index[offset:limit]

    return pages, index

def pagessearch(index,url):
    req = request.values
    page = 1
    if 'p' in req and req['p']:
        page = int(req['p'])

    page_params = {
        'total_count': index.count(),
        "page_size": 8,
        'page': page,
        'url': "/index?"+url,
    }
    pages = iPagenation(page_params)
    # -1 - 30,30 - 60 ,60 - 90
    offset = (page - 1) * page_params['page_size']
    limit = page * page_params['page_size']
    index = index[offset:limit]

    return pages, index



@index_page.route("/index1")
def index():
    classone = Document.query.filter_by(classone="通知规定").order_by(Document.created_time.desc())
    pagesone, classone = pagesadmin(classone)
    classtwo = Document.query.filter_by(classone="制度流程").order_by(Document.created_time.desc())
    pagestwo, classtwo = pagesadmin(classtwo)
    classthree = Document.query.filter_by(classone="承建商库").order_by(Document.created_time.desc())
    pagesthree, classthree = pagesadmin(classthree)

    return ops_render( "index.html",{"classone":classone,"classtwo":classtwo,"classthree":classthree,"pagesone":pagesone,"pagestwo":pagestwo,"pagesthree":pagesthree})



@index_page.route("/search")
def search():
    title = request.args.get("title")
    classone = Document.query.filter_by(type="通知规定").all()
    classtwo = Document.query.filter_by(type="制度流程").all()
    classthree = Document.query.filter_by(type="承建商库").all()
    top = Document.query.filter_by(title=title).first()
    top1 = Document.query.filter_by(weight="置顶").first()
    toptitle = top1.title
    return ops_render( "index.html",{"classone":classone,"classtwo":classtwo,"classthree":classthree,"top":top,"toptitle":toptitle})



# 上传文件
@index_page.route('/upload', methods=['GET', 'POST'])
def uploadfile():
    # 从前端获取文件
    f = request.files.getlist('files')
    classone = request.form.get('classone')
    access_key = 'po98t2WFtFrNvPJ_bZTFobndhhKhQUk6mrXq2NX1'
    secret_key = 'DbooW85jje3wtiDP0se_JLL76PjgLGc-3p2fiEaZ'



    # 构建鉴权对象
    q = Auth(access_key, secret_key)
    # 要上传的空间
    bucket_name = 'library-filestore'
    # http://+外链域名
    domian_name = 'http://libraryfilestore.guanzhuoran.cn/'
    # 生成上传 Token，可以指定过期时间等

    for item in f:
        file = item.read()
        name = item.filename
        uploadname = name
        token = q.upload_token(bucket_name, uploadname, 3600)
        ret, info = put_data(token, uploadname, data=file)
        url = domian_name + uploadname

        name, type1 = os.path.splitext(name)
        type = type1.replace(".", "")
        size = len(file) / 1000000
        size = round(size, 2)

        detail = Document()
        detail.title = name
        detail.type = type
        detail.classone = classone
        detail.url = url
        detail.mark = 1
        detail.size = size
        detail.created_time = detail.updated_time = getCurrentTime()
        db.session.add(detail)
        db.session.commit()

    return ops_renderJSON(msg="上传成功")